
using System.Web.UI.HtmlControls;
using System;
using System.Configuration;
using System.Drawing;
using System.Data;
using System.Web.UI.WebControls;
using Microsoft.VisualBasic;
using System.Collections;
using System.Web.UI;
using System.Web;


using System.IO;
using System.Text;
//using com.ddsc.database;
using DDSC.OracleADO;
using com.ddsc.common;
using SMAS;
using System.Resources;

namespace smasb2b
{
	public class VNDW0030 : BasePage
	{
		//----------------------------------------------------------------------------------------------------------
		//版本  修改日期  修改人員  修改案號    修　改　內　容
		//－－  －－－－  －－－－  －－－－－  －－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－
		//0006  20110915  walter    P3511M0122-報價呈案程式修改案
		//----------------------------------------------------------------------------------------------------------
		
#region  Web Form 設計工具產生的程式碼
		
		//此為 Web Form 設計工具所需的呼叫。
		[System.Diagnostics.DebuggerStepThrough()]private void InitializeComponent()
		{
			
		}
		protected System.Web.UI.HtmlControls.HtmlTable DataGrid;
		protected System.Web.UI.HtmlControls.HtmlTableRow DataGrid_foot;
		protected System.Web.UI.WebControls.Label lblCODE_SEQ1;
		protected System.Web.UI.WebControls.Label lblMATERIAL_CLASS;
		protected ddscControlLibrary.InputBoxCtrl txtENGLISH_SHORTNAME;
		protected System.Web.UI.WebControls.Label lblCODE;
		protected System.Web.UI.WebControls.Label lblSTATUS;
		protected System.Web.UI.HtmlControls.HtmlInputImage Image1;
		protected System.Web.UI.HtmlControls.HtmlTable Table1;
		protected ddscControlLibrary.InputBoxCtrl PageIndex;
		protected ddscControlLibrary.InputBoxCtrl txtCODE_SEQ;
		protected ddscControlLibrary.InputBoxCtrl GotoPage;
		protected ddscControlLibrary.InputBoxCtrl PageCount;
		protected System.Web.UI.WebControls.Label lblENGLISH_SHORTNAME1;
		protected System.Web.UI.WebControls.Label lblENGLISH_SHORTNAME;
		protected System.Web.UI.WebControls.Label lblCODE1;
		protected ddscControlLibrary.SubmitButtonCtrl btnAdd;
		protected System.Web.UI.WebControls.Label lblVNDW0030;
		protected System.Web.UI.HtmlControls.HtmlImage IMG1;
		protected System.Web.UI.WebControls.ImageButton btnPREV;
		protected System.Web.UI.WebControls.ImageButton BtnGo;
		protected System.Web.UI.WebControls.ImageButton BtnNext;
		protected ddscControlLibrary.SubmitButtonCtrl btnSearch;
		protected System.Web.UI.WebControls.Label lblPAGE;
		protected System.Web.UI.WebControls.Label lblTOTAL;
		protected System.Web.UI.WebControls.Label lblPAGE1;
		protected System.Web.UI.WebControls.Label lblPAGEINDEX;
		protected System.Web.UI.WebControls.Label lblPRINTPAGE;
		protected System.Web.UI.WebControls.Label lblMATERIAL_CLASS1;
		protected System.Web.UI.WebControls.DropDownList drpVENDOR_KIND;
		protected System.Web.UI.WebControls.Label lblCLASS;
		protected System.Web.UI.WebControls.Label lblPURCHASE_REMARK;
		protected System.Web.UI.WebControls.Label lblVNDD030_REMARK;
		
		//注意: 下列預留位置宣告是 Web Form 設計工具需要的項目。
		//請勿刪除或移動它。
		private System.Object designerPlaceholderDeclaration;
		
		private void Page_Init(System.Object sender, System.EventArgs e)
		{
			//CODEGEN: 此為 Web Form 設計工具所需的方法呼叫
			//請勿使用程式碼編輯器進行修改。
			InitializeComponent();
		}
		
#endregion
		
#region 呼叫後端method
		
		public DataSet VND_B2B_CLASS()
		{
			DataSet dsReturn = default(DataSet);
			try
			{
				// 查詢資料總筆數之 sql command
				
				
				StringBuilder sb = new StringBuilder();
				sb.Append("Select  B2B_CLASS_CODE,(B2B_CLASS_CODE ||\'-\'||DESCRIPTION_CHINESE)DESCRIPTION_CHINESE,(B2B_CLASS_CODE ||\'-\'||DESCRIPTION_ENGLISH)DESCRIPTION_ENGLISH  From VND_B2B_CLASS Where status = \'1\' ");
				
				// DataTable Name
				string dtName = "VND_B2B_CLASS";
				// Sql command
				string sqlCommands = sb.ToString();
				// create database access object
				dbAccess dba = new dbAccess();
				dsReturn = dba.runExecuteQuery(dtName, sqlCommands);
			}
			catch (Exception ex)
			{
				dsReturn = new DataSet("VND_B2B_CLASS");
				dsReturn.Tables.Add(createErrorTable(ex.Message));
			}
			
			return dsReturn;
		}
		
		private DataTable createErrorTable(string mError)
		{
			dbReturnInfo dbInfo = new dbReturnInfo("info");
			dbInfo.AddNewRow();
			dbInfo.setReturnDataValue("F", System.Convert.ToString(999), 0, mError);
			return dbInfo.getReturnDataTable();
		}
		
		private bool getCLASS()
		{
			bool result = false;
			int i;
			try
			{
				DataSet ds = VND_B2B_CLASS();
				// 將資料寫入下拉式選單
				if (Session["langauage"] == "zh-tw")
				{
					
					if (!(ds == null) && ds.Tables.Count > 0)
					{
						if (!(ds == null) && ds.Tables[0].Rows[0]["ReturnCode"] != "100" && ds.Tables[0].Rows[0]["ReturnCode"] != "999")
						{
							drpVENDOR_KIND.DataSource = ds.Tables["VND_B2B_CLASS"];
							drpVENDOR_KIND.DataTextField = "DESCRIPTION_CHINESE";
							drpVENDOR_KIND.DataValueField = "B2B_CLASS_CODE";
							drpVENDOR_KIND.DataBind();
							drpVENDOR_KIND.Items.Insert(0, new ListItem("ALL", ""));
						}
						else
						{
							drpVENDOR_KIND.Items.Insert(0, new ListItem("", ""));
							
						}
						
					}
				}
				else
				{
					if (!(ds == null) && ds.Tables.Count > 0)
					{
						if (!(ds == null) && ds.Tables[0].Rows[0]["ReturnCode"] != "100" && ds.Tables[0].Rows[0]["ReturnCode"] != "999")
						{
							drpVENDOR_KIND.DataSource = ds.Tables["VND_B2B_CLASS"];
							drpVENDOR_KIND.DataTextField = "DESCRIPTION_ENGLISH";
							drpVENDOR_KIND.DataValueField = "B2B_CLASS_CODE";
							drpVENDOR_KIND.DataBind();
							drpVENDOR_KIND.Items.Insert(0, new ListItem("ALL", ""));
						}
						else
						{
							drpVENDOR_KIND.Items.Insert(0, new ListItem("", ""));
							
						}
						
					}
					
				}
				
			}
			catch (Exception ex)
			{
				showException(ex.Message);
			}
			finally
			{
			}
			return result;
		}
		
#endregion

        public void DataGrid_DataBind()
        {
            //變數宣告
            VENDOR LoginUser = GetLoginUser();
            DataSet ds = default(DataSet);
            dbAccess dba = new dbAccess();
            ArrayList m_arrParam = new ArrayList();
            string strCondition = "";
            int i = 0;
            int count = 3;
            string[] str_SQL_a = new string[count + 1];
            string[] str_SQL_b = new string[count + 1];

            try
            {
                //目前頁次指定Goto
                GotoPage.Text = (dynamic)PageIndex.Text;
                string SqlCommandText = "";
                //預設查詢語法
                if (Session["langauage"] == "zh-tw")
                {
                    //P3511M0122-報價呈案程式修改案
                    //SqlCommandText = "SELECT a.CODE,decode(a.PURCHASE_NO,'','','*')PURCHASE_REMARK,a.ENGLISH_SHORTNAME,(a.CLASS||'-'||b.class_name) CLASS,(a.CLASS||'-'||b.class_name)class_name ,(a.status||'-'||decode(a.status,'9','已審核','未審核')) status  FROM  B2B_MATERIAL a ,(SELECT distinct  B2B_CLASS_CODE CLASS ,DESCRIPTION_CHINESE CLASS_NAME from VND_B2B_CLASS)b where a.class=b.class(+) AND A.STATUS<>'9' and VENDOR_NO='" & LoginUser.VENDOR_NO & "'  "
                    //SqlCommandText = "SELECT a.CODE,decode(a.PURCHASE_NO,\'\',\'\',\'*\')PURCHASE_REMARK,a.ENGLISH_SHORTNAME,(a.CLASS||\'-\'||b.class_name) CLASS,(a.CLASS||\'-\'||b.class_name)class_name ,(a.status||\'-\'||decode(a.status,\'9\',\'已審核\',\'1\',\'未審核\',\'審核中\')) status  FROM  B2B_MATERIAL a ,(SELECT distinct  B2B_CLASS_CODE CLASS ,DESCRIPTION_CHINESE CLASS_NAME from VND_B2B_CLASS)b where a.class=b.class(+) AND A.STATUS<>\'9\' and VENDOR_NO=\'" + LoginUser.VENDOR_NO + "\'  ";
                    SqlCommandText = "SELECT a.CODE,decode(a.PURCHASE_NO,\'\',\'\',\'*\')PURCHASE_REMARK,a.ENGLISH_SHORTNAME,(a.CLASS||\'-\'||b.class_name) CLASS,(a.CLASS||\'-\'||b.class_name)class_name ,(a.status||\'-\'||decode(a.status,\'9\',\'已審核\',\'1\',\'未審核\',\'審核中\')) status  FROM  B2B_MATERIAL a ,(SELECT distinct  B2B_CLASS_CODE CLASS ,DESCRIPTION_CHINESE CLASS_NAME from VND_B2B_CLASS)b where a.class=b.class(+) AND A.STATUS<>\'9\' and VENDOR_NO=:VENDORNO  ";

                }
                else
                {
                    //P3511M0122-報價呈案程式修改案
                    //SqlCommandText = "SELECT a.CODE,decode(a.PURCHASE_NO,'','','*')PURCHASE_REMARK,a.ENGLISH_SHORTNAME,(a.CLASS||'-'||b.class_name) CLASS,(a.CLASS||'-'||b.class_name)class_name ,(a.status||'-'||decode(a.status,'9','已審核','未審核')) status  FROM  B2B_MATERIAL a ,(SELECT distinct  B2B_CLASS_CODE CLASS ,DESCRIPTION_ENGLISH CLASS_NAME from VND_B2B_CLASS)b where a.class=b.class(+) AND A.STATUS<>'9' and VENDOR_NO='" & LoginUser.VENDOR_NO & "'  "
                    //SqlCommandText = "SELECT a.CODE,decode(a.PURCHASE_NO,\'\',\'\',\'*\')PURCHASE_REMARK,a.ENGLISH_SHORTNAME,(a.CLASS||\'-\'||b.class_name) CLASS,(a.CLASS||\'-\'||b.class_name)class_name ,(a.status||\'-\'||decode(a.status,\'9\',\'已審核\',\'1\',\'未審核\',\'審核中\')) status  FROM  B2B_MATERIAL a ,(SELECT distinct  B2B_CLASS_CODE CLASS ,DESCRIPTION_ENGLISH CLASS_NAME from VND_B2B_CLASS)b where a.class=b.class(+) AND A.STATUS<>\'9\' and VENDOR_NO=\'" + LoginUser.VENDOR_NO + "\'  ";
                    SqlCommandText = "SELECT a.CODE,decode(a.PURCHASE_NO,\'\',\'\',\'*\')PURCHASE_REMARK,a.ENGLISH_SHORTNAME,(a.CLASS||\'-\'||b.class_name) CLASS,(a.CLASS||\'-\'||b.class_name)class_name ,(a.status||\'-\'||decode(a.status,\'9\',\'已審核\',\'1\',\'未審核\',\'審核中\')) status  FROM  B2B_MATERIAL a ,(SELECT distinct  B2B_CLASS_CODE CLASS ,DESCRIPTION_ENGLISH CLASS_NAME from VND_B2B_CLASS)b where a.class=b.class(+) AND A.STATUS<>\'9\' and VENDOR_NO=:VENDORNO  ";

                }

                //string SqlCommandCount = "SELECT count(CODE) as row_total_count  FROM B2B_MATERIAL a where STATUS<>\'9\'AND VENDOR_NO=\'" + LoginUser.VENDOR_NO + "\'  ";
                string SqlCommandCount = "SELECT count(CODE) as row_total_count  FROM B2B_MATERIAL a where STATUS<>\'9\'AND VENDOR_NO=:VENDORNO  ";

                //組合條件字串
                str_SQL_a[0] = txtCODE_SEQ.Text.Replace(",", "\'\'");
                //str_SQL_b[0] = string.Format("CODE = \'{0}\' and ", str_SQL_a[0]);
                str_SQL_b[0] = "CODE = :SQLA0 and ";
                str_SQL_a[1] = txtENGLISH_SHORTNAME.Text.Replace(",", "\'\'");
                //str_SQL_b[1] = string.Format("ENGLISH_SHORTNAME like \'{0}%\' and ", str_SQL_a[1]);
                str_SQL_b[1] = "ENGLISH_SHORTNAME like :SQLA1 and ";
                str_SQL_a[2] = drpVENDOR_KIND.SelectedValue;
                //str_SQL_b[2] = string.Format("a.CLASS = \'{0}\' and ", str_SQL_a[2]);
                str_SQL_b[2] = "a.CLASS = :SQLA2 and ";
                //查詢條件組合
                for (i = 0; i <= count - 1; i++)
                {
                    if (str_SQL_a[i].Trim() != "")
                    {
                        strCondition = strCondition + str_SQL_b[i];
                    }
                }

                if (!String.IsNullOrEmpty(strCondition))
                {
                    strCondition = " AND " + strCondition.Substring(0, strCondition.Length - 5);
                    // strCondition = " where VENDOR_NO='" & LoginUser.VENDOR_NO & "'" + strCondition.Substring(0, strCondition.Length - 3)

                }

                int intCount = 0;
                int size = PageSize; // Update by titan 由BasePage設定
                m_arrParam.Add(dba.GetOracleParam(":VENDORNO", LoginUser.VENDOR_NO));
                if (str_SQL_a[0].Trim() != "")
                    m_arrParam.Add(dba.GetOracleParam(":SQLA0", str_SQL_a[0]));
                if (str_SQL_a[1].Trim() != "")
                    m_arrParam.Add(dba.GetOracleParam(":SQLA1", "%" + str_SQL_a[1] + "%"));
                if (str_SQL_a[2].Trim() != "")
                    m_arrParam.Add(dba.GetOracleParam(":SQLA2", str_SQL_a[2]));
                // 計算資料總筆數
                //DataSet dsCount = dba.runExecuteQuery("Vendor_Count", SqlCommandCount + strCondition);
                DataSet dsCount = dba.runExecuteQuery("Vendor_Count", SqlCommandCount + strCondition, m_arrParam);
                if (dsCount.Tables[0].Rows[0]["ReturnCode"] == "000")
                {
                    intCount = int.Parse(System.Convert.ToString(dsCount.Tables["Vendor_Count"].Rows[0]["row_total_count"]));
                    PageCount.Text = System.Convert.ToString(Math.Ceiling(System.Convert.ToDecimal(double.Parse((intCount).ToString()) / size)));
                }

                //Update By Titan 處理當總頁次小於目前頁次
                if ((int.Parse(PageCount.Text) < int.Parse(PageIndex.Text)) && int.Parse(PageCount.Text) > 0)
                {
                    // 總筆數(intCount)小於一頁，從第0筆開始顯示
                    PageIndex.Text = Server.HtmlEncode(PageCount.Text);
                    GotoPage.Text = Server.HtmlEncode(PageCount.Text);
                }
                // Update by titan 由BasePage設定
                // absolute position
                int position = ((int.Parse(PageIndex.Text)) - 1) * size;
                int maxrecord = position + size;


                //控制分頁按鈕
                ControlPageButton(DataGrid_foot);

                //執行資料庫語法
                //ds = dba.runExecuteQuery("B2B_MATERIAL", SqlCommandText + strCondition + " ORDER BY a.CODE ", maxrecord, position);
                ds = dba.runExecuteQuery("B2B_MATERIAL", SqlCommandText + strCondition + " ORDER BY a.CODE ", position, maxrecord, m_arrParam);

                if (!(ds == null) && ds.Tables["info"].Rows[0]["ReturnCode"] != "100" && ds.Tables["info"].Rows[0]["ReturnCode"] != "999")
                {
                    HtmlTableRow tr = (HtmlTableRow)(DataGrid.FindControl("DataGrid_head"));
                    ArrayList ArrKey = new ArrayList();
                    ArrKey.Add("CODE");
                    ArrKey.Add("PURCHASE_REMARK");
                    //ArrKey.Add("ENGLISH_SHORTNAME")
                    ArrKey.Add("CLASS");
                    ArrKey.Add("STATUS");
                    DataGrid_Bind_VNDW0030(DataGrid, ArrKey, ds.Tables["B2B_MATERIAL"]);
                    //控制

                    //For i = 0 To ds.Tables("B2B_MATERIAL").Rows.Count - 1
                    //    If ds.Tables("B2B_MATERIAL").Rows(i)("status").ToString.Substring(0, 1) = "9" Then
                    //        DataGrid.Rows(i + 1).Cells(1).Controls.Clear()
                    //        DataGrid.Rows(i + 1).Cells(2).Controls.Clear()
                    //    End If

                    //Next
                }
                else
                {
                    ResourceManager rm = GetResourceManager();
                    showException(rm.GetString("MSG069"));
                }

            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                ds.Dispose();
            }
        }
		
		private void BtnNext_Click(System.Object sender, System.Web.UI.ImageClickEventArgs e)
		{
			try
			{
				PageIndex.Text = ((int.Parse(PageIndex.Text)) + 1).ToString();
				DataGrid_DataBind();
			}
			catch (Exception ex)
			{
				showException(ex.Message);
			}
		}
		
		private void btnPREV_Click(System.Object sender, System.Web.UI.ImageClickEventArgs e)
		{
			try
			{
				PageIndex.Text = ((int.Parse(PageIndex.Text)) - 1).ToString();
				DataGrid_DataBind();
			}
			catch (Exception ex)
			{
				showException(ex.Message);
			}
			
		}
		
		private void BtnGo_Click(System.Object sender, System.Web.UI.ImageClickEventArgs e)
		{
			ResourceManager rm = GetResourceManager();
			try
			{
				PageIndex.Text = Server.HtmlEncode(GotoPage.Text);
				DataGrid_DataBind();
			}
			catch (Exception)
			{
				showException(rm.GetString("MSG057"));
			}
			
		}
		
		private void btnSearch_Click(System.Object sender, System.EventArgs e)
		{
			try
			{
				PageIndex.Text = System.Convert.ToString(1);
				DataGrid_DataBind();
			}
			catch (Exception ex)
			{
				showException(ex.Message);
			}
			
		}
		
		private void Page_Load(System.Object sender, System.EventArgs e)
		{
			//在這裡放置使用者程式碼以初始化網頁
			try
			{
				if (Session["langauage"] == "zh-tw")
				{
					
					this.btnAdd.Style.Add("width", "85PX");
				}
				else
				{
					this.btnAdd.Style.Add("width", "130PX");
				}
				if (!Page.IsPostBack)
				{
					
					getCLASS();
				}
				else
				{
					if (IsReSubmit)
					{
						DataGrid_DataBind();
					}
				}
			}
			catch (Exception ex)
			{
				showException(ex.Message);
			}
            this.BtnNext.Click += BtnNext_Click;
            this.BtnGo.Click += BtnGo_Click;
            this.btnPREV.Click += btnPREV_Click;
            this.btnSearch.Click += btnSearch_Click;

			
		}
		
		//將資料呈現於畫面
		public void DataGrid_Bind_VNDW0030(HtmlTable DataGrid, ArrayList ArrKey, DataTable DataSource)
		{
			
			HtmlTableRow tr = (HtmlTableRow) (DataGrid.FindControl("DataGrid_head"));
			int i = 0;
			int j = 0;
			int k = 0;
			string Url = "";
			string className = "";
			ResourceManager rm = GetResourceManager();
			try
			{
				for (i = 0; i <= DataSource.Rows.Count - 1; i++)
				{
					HtmlTableRow htr = new HtmlTableRow();
					if (i % 2 == 0)
					{
						className = "evenRow";
					}
					else
					{
						className = "oddRow";
					}
					htr.Attributes.Add("class", className);
					DataGrid.Rows.Add(htr);
					for (j = 0; j <= tr.Cells.Count - 1; j++)
					{
						HtmlTableCell hd = new HtmlTableCell();
						//P3511M0122-報價呈案程式修改案
						//If DataSource.Rows(i)("status").ToString.Substring(0, 1) = "9" Then
						if (DataSource.Rows[i]["status"].ToString().Substring(0, 1) == "3" 
							|| DataSource.Rows[i]["status"].ToString().Substring(0, 1) == "4" 
							|| DataSource.Rows[i]["status"].ToString().Substring(0, 1) == "5" 
							|| DataSource.Rows[i]["status"].ToString().Substring(0, 1) == "6" 
							|| DataSource.Rows[i]["status"].ToString().Substring(0, 1) == "7" 
							|| DataSource.Rows[i]["status"].ToString().Substring(0, 1) == "8" 
							|| DataSource.Rows[i]["status"].ToString().Substring(0, 1) == "9")
						{
							
							tr.Cells[1].Attributes["class"] = "else";
							tr.Cells[2].Attributes["class"] = "else";
						}
						else
						{
							tr.Cells[1].Attributes["class"] = "link";
							tr.Cells[2].Attributes["class"] = "link";
						}
						switch (tr.Cells[j].Attributes["class"].ToLower())
						{
							case "link":
								Url = string.Format("{0}?action_type={1}&GUID={2}", tr.Cells[j].Attributes["url"].ToString(), tr.Cells[j].ID, Guid.NewGuid());
								for (k = 0; k <= ArrKey.Count - 1; k++)
								{
									Url += "&" + ArrKey[k] + "=" + DataSource.Rows[i][ArrKey[k].ToString()].ToString();
								}
								
								hd.Width = tr.Cells[j].Width;
								hd.InnerHtml = string.Format("<a href=\"javascript:window.showModalDialog(\'{0}\',this,\'{1}\', true)\">{2}</a>", Url, "dialogWidth=800px;dialogHeight=740px;center=yes;border=thin;help=no;status=no;resizable=no;", rm.GetString(System.Convert.ToString(tr.Cells[j].ID)));
								break;
							case "tdtitle":
								hd.Width = tr.Cells[j].Width;
								hd.Attributes.Add("class", "detail");
								hd.InnerText = System.Convert.ToString(DataSource.Rows[i][tr.Cells[j].Controls[1].ID.Replace("lbl", "")].ToString());
								break;
							default:
								hd.Width = tr.Cells[j].Width;
								hd.InnerText = rm.GetString(System.Convert.ToString(tr.Cells[j].ID));
								break;
						}
						DataGrid.Rows[DataGrid.Rows.Count - 1].Cells.Add(hd);
						
					}
				}
			}
			catch (Exception ex)
			{
				throw (ex);
			}
			
			
		}
		
		
		
	}
	
}
